//
//  LLFocusListViewController.m
//  LoveLove
//
//  Created by 达迩科技 on 2019/4/9.
//  Copyright © 2019 wkf. All rights reserved.
//

#import "LLFocusListViewController.h"
#import "LLFocusListTableViewCell.h"
#import "LLUserCenterVCViewController.h"

@interface LLFocusListViewController ()


@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation LLFocusListViewController

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
}

#pragma mark - 显示视图操作方法
- (void)configTableView {
    self.needRefresh = NO;
    [super configTableView];
    self.tableView.backgroundColor = LLColorRGB(255);
    self.tableView.mj_h = LLScreenHeight-self.navH-100;
    [self.tableView registerCellByClassName:@"LLFocusListTableViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self sendFansRequst];
    }];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page= 1;
        [self.dataArray removeAllObjects];
        [self sendFansRequst];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LLFocusListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLFocusListTableViewCell"];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;//无高亮选中
    cell.backgroundColor = [UIColor whiteColor];
    [cell configFocusCell:self.dataArray[indexPath.row] andType:self.type];
    cell.focusButton.tag = 200+ indexPath.row;
    [cell.focusButton addTarget:self action:@selector(guanZhu:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 56;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 0.0000001;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0000001;
}

//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *viewHeaderInSection = [[UIView alloc] init];
//    viewHeaderInSection.backgroundColor = LLWhiteColor;
//    UILabel *label = [LLInitBaseViewTool labelWithText:[NSString stringWithFormat:@"%@人关注我",@"153"] textColor:LLColorRGB51 font:12 textAlignment:NSTextAlignmentLeft];
//    label.frame = CGRectMake(15, 0, LLScreenWidth-30, 30);
//    [viewHeaderInSection addSubview:label];
//    return viewHeaderInSection;
//}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSDictionary *dict = self.dataArray[indexPath.row];

    LLUserCenterVCViewController *vc = [LLUserCenterVCViewController new];
    vc.user_id = dict[@"u_id"];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark---粉丝
-(void)sendFansRequst{
    LLUserInfo *user = [LLUserInfo sharedUser];
    
    NSDictionary *dic = @{@"edu_id":@"19",@"user_random":user.user_random,@"user_id":user.user_id,@"type_id":@(self.type),@"page":@(self.page)};
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        self.page++;
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        NSArray *dataArr = result[@"data"];
        if (dataArr.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return ;
        }
        [self.dataArray addObjectsFromArray:dataArr];
        [self.tableView reloadData];
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
    
}

-(void)guanZhu:(UIButton *)button{
//    button.selected = !button.selected;
    //edu_id:15(固定值)
    //user_id:  用户id
    //new_uid:  新闻发布者的id     user_random: 随机数
    //is_follow: 1是关注，0是取消关注
    NSInteger islove;
//    if(button.selected){
//        islove = 1;
//    }else{
//        islove = 0;
//    }
    
    NSInteger index = button.tag - 200;
    NSDictionary *dict = self.dataArray[index];
    if ([dict[@"is_follow"] integerValue] == 1) {
        islove = 0;
    }else{
        islove = 1;
    }
    
    LLUserInfo *user = [LLUserInfo sharedUser];
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary: @{@"edu_id":@"15"}];
    [dic setObject:user.user_id forKey:@"user_id"];
    [dic setObject:user.user_random forKey:@"user_random"];
    [dic setObject:dict[@"u_id"] forKey:@"new_uid"];
    [dic setObject:@(islove) forKey:@"is_follow"];
    [self showSVProgressHUD];
    [LLHttpTool post:LLHOST headParams:nil params:dic success:^(NSDictionary *result) {
        //        [SVProgressHUD dismiss];
        if (islove == 1) {
            [self showSVProgressHUDInfoWithsStatus:@"关注成功"];
        }else
            [self showSVProgressHUDInfoWithsStatus:@"取消关注"];
        [self.dataArray removeAllObjects];
        self.page = 1;
        [self sendFansRequst];
        
        
    } failure:^(NSString *msg) {
        [SVProgressHUD showErrorWithStatus:msg];
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
